const Task = require('../models/task')
const User = require('../models/user')
const mongoose = require('mongoose')

exports.deanTasks = async function loop(task) {
  var arr = []
  async function taskLoop(loop) {
    for (let i = 0; i < loop.length; i++) {
      let task1 = await Task.find({
          parentId: loop[i]._id,
        })
        .populate('worker', ['name'])
        .populate('creator', ['name'])
        .populate('parentId', ['taskname'])
        .sort({
          _id: -1
        })
        .lean()
      
      //查询上传文件人
      // for (let i in task1) {
      //   for (let a in task1[i].fileAddress) {
      //     let arr = task1[i].fileAddress[a]
      //     if (arr.userId) {
      //       let user = await User.findById(arr.userId)
      //       arr.user = user
      //     }
      //   }
      // }

      loop[i].children = task1
      var loop2 = loop[i].children
      if (task1.length == 0) {
        delete loop[i].children
      }
      await taskLoop(loop2)
    }
    if (loop.length == 0) {
      arr.push(loop)
    }
  }
  await taskLoop(task)
  task = arr[arr.length - 1]
}

exports.deanTask = async function loop(task) {
  var arr = []
  async function taskLoop(loop) {
    for (let i = 0; i < loop.length; i++) {
      let task1 = await Task.find({
          parentId: loop[i]._id,
        })
        .populate('worker', ['name'])
        .populate('creator', ['name'])
        .populate('parentId', ['taskname'])
        .sort({
          _id: -1
        })
        .lean()

      //查询上传文件人
      // for (let i in task1) {
      //   for (let a in task1[i].fileAddress) {
      //     let arr = task1[i].fileAddress[a]
      //     if (arr.userId) {
      //       let user = await User.findById(arr.userId)
      //       arr.user = user
      //     }
      //   }
      // }
      
      loop[i].children = task1
      var loop2 = loop[i].children
      if (task1.length == 0) {
        delete loop[i].children
      }
      await taskLoop(loop2)
    }
    if (loop.length == 0) {
      arr.push(loop)
    }
  }
  await taskLoop(task)
  var task = arr[arr.length - 1]
}

exports.deleteTask = async function loop(task) {
  async function taskLoop(loop) {
    for (let i = 0; i < loop.length; i++) {
      let task1 = await Task.find({
          parentId: loop[i]._id,
        })
        .populate('worker', ['name'])
        .populate('creator', ['name'])
        .populate('parentId', ['taskname'])
        .sort({
          _id: -1
        })
        .lean()
      loop[i].children = task1
      var loop2 = loop[i].children
      if (task1.length == 0) {
        delete loop[i].children
      }
      await taskLoop(loop2)
    }
    if (loop.length != 0) {
      for (value of loop) {
        var deleteTask = await Task.findById(value._id)
        await deleteTask.remove()
      }
    }
  }
  await taskLoop(task)
}

// //计算进度
// exports.countProcess = async (parentId) => {
//   //判断是否存在父级
//   if (parentId) {
//     //查询任务
//     let parent = await Task.findById(parentId).lean()
//     //查询子任务(数组)
//     let sons = await Task.find({
//       parentId: parent._id
//     })
//     //所有子任务的进度之和
//     let sum = 0
//     for (let i in sons) {
//       sum += parseInt(sons[i].process)
//     }
//     //子任务数量
//     let quantity = sons.length
//     // console.log(sum);
//     parent.process = parseInt(sum / quantity)
//     await Task.findByIdAndUpdate(parent._id, parent)

//     await arguments.callee(parent.parentId)
//   }
// }